package cn.play.cardgame.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import cn.play.cardgame.util.CryptUtils;

public class FaPai {

	static String[] suits = { "Heixin", "Hongxin", "Fangkuai", "Caohua" }; // 黑桃
	// 红桃
	// 方块
	// 梅花
	public static boolean isHeixin(String huase) {
		return huase.equals(suits[0]);
	}

	public static boolean isHongxin(String huase) {
		return huase.equals(suits[1]);
	}

	public static boolean isFangkuai(String huase) {
		return huase.equals(suits[2]);
	}

	public static boolean isCaohua(String huase) {
		return huase.equals(suits[3]);
	}

	public static boolean isKing(String huase) {
		return huase.equals("a") || huase.equals("b");
	}

	// 历史里的牌 type 1:result 2:history 3:count
	public static int getImage(Context context, String value, String huase, int type) {
		String prefix = huase;
		if (huase.equals("Heixin")) {
			prefix = "heixin";
		} else if (huase.equals("Hongxin")) {
			prefix = "hongxin";
		} else if (huase.equals("Fangkuai")) {
			prefix = "fangkuai";
		} else if (huase.equals("Caohua")) {
			prefix = "caohua";
		} else
		if (huase.equals("b")) {
			if (type == 1)
				return getResource(context, "b_result");
			else if (type == 2)
				return getResource(context, "b");
		} else if (huase.equals("a")) {
			if (type == 1)
				return getResource(context, "a_result");
			else if (type == 2)
				return getResource(context, "a");
		}

		if (type == 1)
			return getResource(context, prefix + value + "_result");
		else if (type == 2)
			return getResource(context, prefix + value);
		return type;
	}

	/**
	 * 根据图片名称获取图片的资源id的方法
	 *
	 *
	 * @param imageName
	 * @return
	 */
	public static int getResource(Context context, String imageName) {
		int resId = context.getResources().getIdentifier(imageName, "drawable", context.getPackageName());
		return resId;
	}

	/**
	 *
	 * @Description : 身份验证token值算法： 算法是：将特定的某几个参数一map的数据结构传入，
	 *              进行字典序排序以后进行md5加密,32位小写加密；
	 * @Method_Name : authentication
	 * @param token
	 *            请求传过来的token
	 * @param srcData
	 *            约定用来计算token的参数
	 * @return
	 */
	public static String authentication(Map<String, Object> srcData) {
		// 排序，根据keyde 字典序排序
		if (null == srcData) {
			throw new NullPointerException("传入参数为空");
		}
		List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>(
				srcData.entrySet());
		Collections.sort(list, new Comparator<Map.Entry<String, Object>>() {
			// 升序排序
			public int compare(Entry<String, Object> o1,
							   Entry<String, Object> o2) {
				return o1.getKey().compareTo(o2.getKey());
			}
		});

		StringBuffer srcSb = new StringBuffer();
		for (Map.Entry<String, Object> srcAtom : list) {
			srcSb.append(String.valueOf(srcAtom.getValue()));
		}
		System.out.println("身份验证加密前字符串：" + srcSb.toString());
		// 计算token
		String token = CryptUtils.getMD5(srcSb.toString());
		return token;
	}
}
